//
//  BuyViewController.m
//  wtshoppingcite
//
//  Created by TW on 14-11-5.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "BuyViewController.h"
#import "AdispatchView.h"
#import "PaymentViewController.h"
#import "DeliveryViewController.h"

@interface BuyViewController ()<UITableViewDelegate,
                                UITableViewDataSource,
                                deliveryViewDelegate,
                                UITextFieldDelegate,
                                adispathViewDelegate>
{
    UITableView *buyTableView;
}

@end

@implementation BuyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setTitleView:@"立即购买"];
    
    buyTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    buyTableView.dataSource = self;
    buyTableView.delegate = self;
    buyTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    buyTableView.scrollEnabled = NO;

    [self.view addSubview:buyTableView];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, kHeight - 50 - 64, kWidth, 50)];
    [self.view addSubview:view];
    
    UILabel *totalPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,10,80, 40)];
    totalPriceLabel.text = @"应付总价:";
    [view addSubview:totalPriceLabel];
    totalPriceLabel.font = [UIFont systemFontOfSize:15.0f];
    totalPriceLabel.backgroundColor = [UIColor clearColor];
    totalPriceLabel.textColor = UIColorFromRGB(0x9fa0a0);
    
    
    UIImageView *iconImage = [[UIImageView alloc]initWithFrame:CGRectMake(totalPriceLabel.right, 20, 10, 10)];
    iconImage.bottom = totalPriceLabel.bottom -15;
    iconImage.image = [UIImage imageNamed:@"money_code"];
    [view addSubview:iconImage];
    
    
    _totalLabel = [[UILabel alloc]initWithFrame:CGRectMake(iconImage.right, 6, 220, 40)];
    _totalLabel.bottom = totalPriceLabel.bottom;
    _totalLabel.left = iconImage.right;
    _totalLabel.text =self.shoppingModel.nPrice;
    [view addSubview:_totalLabel];
    _totalLabel.font = [UIFont systemFontOfSize:24.0f];
    _totalLabel.backgroundColor = [UIColor clearColor];
    _totalLabel.textColor = MAIN_BLUE_COLOR;
    
    
    UIButton *commitButton = [[UIButton alloc]initWithFrame:CGRectMake(6, 6, 100, 40)];
    commitButton.bottom = _totalLabel.bottom-5;
    commitButton.right = kWidth - 10;
    [commitButton setBackgroundImage:[UIImage imageNamed:@"register_select"] forState:UIControlStateNormal];
    [commitButton setTitle:@"提交订单" forState:UIControlStateNormal];
    [commitButton setFont:[UIFont systemFontOfSize:15]];
    [view addSubview:commitButton];
    [commitButton setTitleColor: [UIColor whiteColor] forState:UIControlStateNormal];
    [commitButton addTarget:self action:@selector(commitAction) forControlEvents:UIControlEventTouchUpInside];

    
    UITapGestureRecognizer *thumbnailTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(thumbnailTapAction)];
    [buyTableView addGestureRecognizer:thumbnailTap];
    
    // Do any additional setup after loading the view.
}


#pragma mark thumbnailTapAction:
-(void)thumbnailTapAction{
    
    [_textField resignFirstResponder];
    [_addAddressButton resignFirstResponder];
    [_subtractButton resignFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    ;
}


- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"shareCellIdentitfer"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"shareCellIdentitfer"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (indexPath.row == 0) {
            
            _titleLabel = [self createLabelWithFrame:CGRectMake(15, 15, 200, 20) text:@"" font:15.0f textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
            _titleLabel.text = self.shoppingModel.title;
            [cell.contentView addSubview:_titleLabel];
            
            
            UILabel *subTitleLabel =[self createLabelWithFrame:CGRectMake(_titleLabel.left, _titleLabel.bottom + 10, kWidth - 40, 60) text:@"" font:13.0f textAli:NSTextAlignmentLeft textColor: UIColorFromRGB(0x9fa0a0)];
            subTitleLabel.numberOfLines = 0;
            subTitleLabel.text = self.shoppingModel.subTitle;
            [cell.contentView addSubview:subTitleLabel];
            
            UIImageView *iconImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 10, 10)];
            iconImage.left = kWidth - 60;
            iconImage.image = [UIImage imageNamed:@"money_code"];
            [cell.contentView addSubview:iconImage];
            
            UILabel *oPirceLabel = [[UILabel alloc]initWithFrame:CGRectMake(iconImage.right,iconImage.top -10, 60, 40)];
            oPirceLabel.bottom = iconImage.bottom + 15;
            oPirceLabel.text =self.shoppingModel.nPrice;

            
            [cell.contentView addSubview:oPirceLabel];
            oPirceLabel.font = [UIFont systemFontOfSize:15.0f];
            oPirceLabel.backgroundColor = [UIColor clearColor];
            oPirceLabel.textColor = MAIN_BLACK_COLOR;
            
            UILabel *bugLabel = [self createLabelWithFrame:CGRectMake(subTitleLabel.left-15,subTitleLabel.bottom + 15, 160, 40) text:@"" font:15.0f textAli:NSTextAlignmentLeft textColor:UIColorFromRGB(0x9fa0a0)];
            bugLabel.text =@"数量:最多购买50件";
            [cell.contentView addSubview:bugLabel];

            _addButton = [[UIButton alloc]initWithFrame:CGRectMake(bugLabel.right, bugLabel.top+3, 30, 30)];
            [_addButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [_addButton setTitleColor: MAIN_BLACK_COLOR forState:UIControlStateNormal];
            _addButton.tag = 100;
            _addButton.left = kWidth -60-40-30-6;
            [_addButton setBackgroundImage:[UIImage imageNamed:@"near_subtractButton"] forState:UIControlStateNormal];
            [cell.contentView addSubview:_addButton];
            
            
            _textField = [[UITextField alloc]initWithFrame:CGRectMake(_addButton.right + 3,bugLabel.top+3, 40, 30)];
            _textField.text = @"1";
             self.i = [_textField.text integerValue];
            _textField.delegate = self;
            _textField.borderStyle = UITextBorderStyleRoundedRect;
            _textField.textAlignment = NSTextAlignmentCenter;
            [cell.contentView addSubview:_textField];
            
            _subtractButton = [[UIButton alloc]initWithFrame:CGRectMake(_textField.right + 3, bugLabel.top+3, 30, 30)];
            [_subtractButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [_subtractButton setTitleColor: MAIN_BLACK_COLOR forState:UIControlStateNormal];
            _subtractButton.tag = 101;
            [_subtractButton setBackgroundImage:[UIImage imageNamed:@"addbutton"] forState:UIControlStateNormal];
            [cell.contentView addSubview:_subtractButton];
            
            
            UIImageView *totalIconImage = [[UIImageView alloc]initWithFrame:CGRectMake(_textField.left + 20, _textField.bottom + 35, 10, 10)];
            totalIconImage.left = kWidth - 100;
            totalIconImage.image = [UIImage imageNamed:@"money_code"];
            [cell.contentView addSubview:totalIconImage];
            
            _totalPirceLabel = [[UILabel alloc]initWithFrame:CGRectMake(totalIconImage.right,totalIconImage.top -10, 220, 40)];
            _totalPirceLabel.bottom = totalIconImage.bottom + 15;
            
            [cell.contentView addSubview:_totalPirceLabel];
            _totalPirceLabel.font = [UIFont systemFontOfSize:15.0f];
            _totalPirceLabel.text = self.shoppingModel.nPrice;
            _totalPirceLabel.backgroundColor = [UIColor clearColor];
            
            _totalPirceLabel.textColor = MAIN_BLACK_COLOR;
            
            
            UILabel *totalLabel = [self createLabelWithFrame:CGRectMake(bugLabel.left,bugLabel.bottom + 10, 120, 40) text:@"" font:15.0f textAli:NSTextAlignmentLeft textColor:UIColorFromRGB(0x9fa0a0)];
            totalLabel.text =@"小计:";
            [cell.contentView addSubview:totalLabel];
            
            UIView *separateView = [[UIView alloc]init];
            separateView.frame = CGRectMake(0,220 - 10, kWidth, 10);
            separateView.backgroundColor = UIColorFromRGB(0x9fa0a0);
            separateView.alpha =0.2;
            [cell.contentView addSubview:separateView];

            
            
        }else if(indexPath.row == 1){
            
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                       
            UILabel *addressLabel = [self createLabelWithFrame:CGRectMake(6, 6,80, 40) text:@"" font:15.0f textAli:NSTextAlignmentLeft textColor:UIColorFromRGB(0x9fa0a0)];
            addressLabel.text = @"收货地址:";
            [cell.contentView addSubview:addressLabel];
            
            
            _address = [self createLabelWithFrame:CGRectMake(addressLabel.right, 0,200, 60) text:@"" font:12.0f textAli:NSTextAlignmentLeft textColor:UIColorFromRGB(0x9fa0a0)];
            _address.numberOfLines = 0;
            [cell.contentView addSubview:_address];

            
            _addAddressButton = [[UIButton alloc]initWithFrame:CGRectMake(6, 6, 160, 40)];
            _addAddressButton.left = kWidth - 170;
            _addAddressButton.tag = 10000;
            [_addAddressButton setTitle:@"新增收货地址" forState:UIControlStateNormal];
            [_addAddressButton setImage:[UIImage imageNamed:@"register_add"] forState:UIControlStateNormal];
            [_addAddressButton setFont:[UIFont systemFontOfSize:15]];
            [cell.contentView addSubview:_addAddressButton];
            [_addAddressButton addTarget:self action:@selector(commonButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            [_addAddressButton setTitleColor: UIColorFromRGB(0x9fa0a0) forState:UIControlStateNormal];
            
            UIView *seperateLine = [[UIView alloc]initWithFrame:CGRectMake(0, 60 - 1, kWidth, 1)];
            seperateLine.backgroundColor = MAIN_GRAY_COLOR;
            [cell.contentView addSubview:seperateLine];
            
        }else if (indexPath.row == 2){
            
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
            
            UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(6, 6,100, 40)];
            timeLabel.text = @"配送时间:";
            [cell.contentView addSubview:timeLabel];
            timeLabel.font = [UIFont systemFontOfSize:15.0f];
            timeLabel.backgroundColor = [UIColor clearColor];
            timeLabel.textColor = UIColorFromRGB(0x9fa0a0);
            
           
            _adispatchButton = [[UIButton alloc]initWithFrame:CGRectMake(6, 6, 180, 40)];
            _adispatchButton.left = timeLabel.right;
            _adispatchButton.tag = 10001;
            [_adispatchButton setTitle:@"工作日、节假日均可送货" forState:UIControlStateNormal];
            [_adispatchButton setFont:[UIFont systemFontOfSize:15]];
            [cell.contentView addSubview:_adispatchButton];
            [_adispatchButton addTarget:self action:@selector(commonButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            [_adispatchButton setTitleColor: UIColorFromRGB(0x9fa0a0) forState:UIControlStateNormal];
            
            UIView *separateView = [[UIView alloc]init];
            separateView.frame = CGRectMake(0,60 - 10, kWidth, 10);
            separateView.backgroundColor = UIColorFromRGB(0x9fa0a0);
            separateView.alpha =0.2;
            [cell.contentView addSubview:separateView];
            
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
            
            if (indexPath.row == 0) {
                return 220;
            }else if (indexPath.row == 1){
                return 60;
            }else if (indexPath.row ==2){
                return 60;
            }else{
                return 0;
            }
}


#pragma mark commitAction
-(void)commitAction{
    if (_address.text == nil || [_address.text  isEqual: @""]) {
        [self showToastMessage:@"请选择收货地址!"];
        
    }else{
        PaymentViewController *paymentVC =[[PaymentViewController alloc]init];
        paymentVC.hidesBottomBarWhenPushed = YES;
        paymentVC.shoppingModel = self.shoppingModel;
        paymentVC.totalPrice =_totalPirceLabel.text;
        paymentVC.type = NSShoppingPayForStyle;
        paymentVC.i = self.i;
        paymentVC.isPurchase = NO;
        [self.navigationController pushViewController:paymentVC animated:YES];
    }
}


//#pragma mark buttonAction
-(void)commonButtonAction:(UIButton *)sender{
    
    if (sender.tag == 10000) {
        
        DeliveryViewController *deliveryVC = [[DeliveryViewController alloc]init];
        deliveryVC.hidesBottomBarWhenPushed = YES;
        deliveryVC.delegate = self;
        [self.navigationController pushViewController:deliveryVC animated:YES];
        
    }else if (sender.tag == 10001){
        
        AdispatchView *adispatchView = [[AdispatchView alloc]initWithFrame:CGRectMake(20, kHeight/2 - 44*5/2, kWidth - 40, 44*5)];
        [adispatchView showView:self.view];
        adispatchView.delegate = self;
        adispatchView.titleArray = @[@"立即配送",@"电话预约",@"只双休日、节假日送货",@"工作日、节假日均可送货",@"只工作日送货"];
        [self.view addSubview:adispatchView];
        
    }
}

#pragma mark deliveryDelegate
-(void)commitAddress:(NSString *)address{
    
    _address.text = address;
    [_addAddressButton setTitle:@"" forState:UIControlStateNormal];
    [_addAddressButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
}

#pragma mark adispatchViewDlegate
-(void)adispathView:(NSString *)title{
    
    [_adispatchButton setTitle:title forState:UIControlStateNormal];
}

-(void)buttonAction:(UIButton *)sender{
    
    if (sender.tag == 100 && self.i > 0) {
        
        self.i = self.i - 1;
        
        [_addButton setBackgroundImage:[UIImage imageNamed:@"subtractButton_select_org.png"] forState:UIControlStateNormal];
        [_subtractButton setBackgroundImage:[UIImage imageNamed:@"addbutton"] forState:UIControlStateNormal];
        
        _totalPirceLabel.text = [NSString stringWithFormat:@"%0.2f",[self.shoppingModel.nPrice floatValue] *self.i];
        
        _textField.text = [NSString stringWithFormat:@"%d",self.i];
        
        _totalLabel.text = _totalPirceLabel.text;
        
        
    }else if (sender.tag == 101){
        
        self.i = self.i + 1;
        
        [_subtractButton setBackgroundImage:[UIImage imageNamed:@"addButton_select_org.png"] forState:UIControlStateNormal];
        [_addButton setBackgroundImage:[UIImage imageNamed:@"near_subtractButton"] forState:UIControlStateNormal];
        
        _totalPirceLabel.text = [NSString stringWithFormat:@"%0.2f",[self.shoppingModel.nPrice floatValue] *self.i];
        
        _textField.text = [NSString stringWithFormat:@"%d",self.i];
        
        _totalLabel.text = _totalPirceLabel.text;
        
        
    }
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    
    self.i = [textField.text integerValue];
    _totalPirceLabel.text = [NSString stringWithFormat:@"%0.2f",[self.shoppingModel.nPrice floatValue] *self.i];
    _totalLabel.text = _totalPirceLabel.text;
    
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [_textField resignFirstResponder];
    return YES;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
